package com.ghyf.mplay.uimanager;

import java.util.ArrayList;
import java.util.ListIterator;


import com.ghyf.mplay.application.BaseCookie;
import com.ghyf.mplay.bean.BaseModuleInfo;
import com.ghyf.mplay.bean.CountDownInfo;
import com.ghyf.mplay.bean.DateInfo;
import com.ghyf.mplay.bean.ExigentInfo;
import com.ghyf.mplay.bean.PicShowInfo;
import com.ghyf.mplay.bean.ScrollInfo;
import com.ghyf.mplay.bean.TextInfo;
import com.ghyf.mplay.bean.TimeInfo;
import com.ghyf.mplay.bean.VideoInfoView;
import com.ghyf.mplay.bean.VideoSurfaceInfo;
//import com.ghyf.mplay.bean.VideoVitamioInfo;
import com.ghyf.mplay.util.LogUtil;
import com.ghyf.mplay.value.ConstantValue;
import com.ghyf.mplay.view.MVideo;
import com.ghyf.mplay.view.MVideoLive;
//import com.ghyf.mplay.view.MYVideo;

import android.app.Activity;
import android.view.View;
import android.view.ViewGroup;
/**
 * 界面显示、界面切换的管理类
 * @author 陈小红
 *
 */
public class ViewManager {
	private static final String TAG="ViewManager";
	private Activity activity;
	private VideoInfoView videoInfo=null;
	private MVideo lVideo=null;
	private VideoSurfaceInfo surfaceInfo=null;
//	private VideoVitamioInfo vitamioInfo=null;
//	private MYVideo myVideo=null;
	private MVideoLive surfaceView=null; 
	private ScrollInfo scrollInfo=null;
	private CountDownInfo countDownInfo=null;
	private DateInfo dateInfo=null;
	private TimeInfo timeInfo=null;
	private TextInfo textInfo=null;
	private ExigentInfo exigentInfo=null;
	private PicShowInfo picShowInfo=null;
	private ViewGroup viewGroup=null;
	
	// 单例模式,保证只有一个实例
	private static ViewManager instance =null;

	/**
	 * 构造方法
	 */
	private ViewManager() {
	}
	
	/**
	 * @return the myVideo
	 */
//	public MYVideo getMyVideo() {
//		return myVideo;
//	}
//
//	/**
//	 * @param myVideo the myVideo to set
//	 */
//	public void setMyVideo(MYVideo myVideo) {
//		this.myVideo = myVideo;
//	}

	/**
	 * @return the surfaceInfo
	 */
	public VideoSurfaceInfo getSurfaceInfo() {
		return surfaceInfo;
	}
	/**
	 * @param surfaceInfo the surfaceInfo to set
	 */
	public void setSurfaceInfo(VideoSurfaceInfo surfaceInfo) {
		this.surfaceInfo = surfaceInfo;
	}
	/**
	 * @return the surfaceView
	 */
	public MVideoLive getSurfaceView() {
		return surfaceView;
	}
	/**
	 * @param surfaceView the surfaceView to set
	 */
	public void setSurfaceView(MVideoLive surfaceView) {
		this.surfaceView = surfaceView;
	}
	/**
	 * @return the lVideo
	 */
	public MVideo getlVideo() {
		return lVideo;
	}
	/**
	 * @param lVideo the lVideo to set
	 */
	public void setlVideo(MVideo lVideo) {
		this.lVideo = lVideo;
	}

	/**
	 * @return the videoInfo
	 */
	public VideoInfoView getVideoInfo() {
		return videoInfo;
	}
	/**
	 * @param videoInfo the videoInfo to set
	 */
	public void setVideoInfo(VideoInfoView videoInfo) {
		this.videoInfo = videoInfo;
	}

	/**
	 * @return the scrollInfo
	 */
	public ScrollInfo getScrollInfo() {
		return scrollInfo;
	}



	/**
	 * @param scrollInfo the scrollInfo to set
	 */
	public void setScrollInfo(ScrollInfo scrollInfo) {
		this.scrollInfo = scrollInfo;
	}



	/**
	 * @return the countDownInfo
	 */
	public CountDownInfo getCountDownInfo() {
		return countDownInfo;
	}
	/**
	 * @param countDownInfo the countDownInfo to set
	 */
	public void setCountDownInfo(CountDownInfo countDownInfo) {
		this.countDownInfo = countDownInfo;
	}
	/**
	 * @return the dateInfo
	 */
	public DateInfo getDateInfo() {
		return dateInfo;
	}

	/**
	 * @param dateInfo the dateInfo to set
	 */
	public void setDateInfo(DateInfo dateInfo) {
		this.dateInfo = dateInfo;
	}

	/**
	 * @return the timeInfo
	 */
	public TimeInfo getTimeInfo() {
		return timeInfo;
	}



	/**
	 * @param timeInfo the timeInfo to set
	 */
	public void setTimeInfo(TimeInfo timeInfo) {
		this.timeInfo = timeInfo;
	}



	/**
	 * @return the textInfo
	 */
	public TextInfo getTextInfo() {
		return textInfo;
	}



	/**
	 * @param textInfo the textInfo to set
	 */
	public void setTextInfo(TextInfo textInfo) {
		this.textInfo = textInfo;
	}



	/**
	 * @return the exigentInfo
	 */
	public ExigentInfo getExigentInfo() {
		return exigentInfo;
	}



	/**
	 * @param exigentInfo the exigentInfo to set
	 */
	public void setExigentInfo(ExigentInfo exigentInfo) {
		this.exigentInfo = exigentInfo;
	}



	/**
	 * @return the picShowInfo
	 */
	public PicShowInfo getPicShowInfo() {
		return picShowInfo;
	}



	/**
	 * @param picShowInfo the picShowInfo to set
	 */
	public void setPicShowInfo(PicShowInfo picShowInfo) {
		this.picShowInfo = picShowInfo;
	}



	/**
	 * @return the viewGroup
	 */
	public ViewGroup getViewGroup() {
		return viewGroup;
	}



	/**
	 * @param viewGroup the viewGroup to set
	 */
	public void setViewGroup(ViewGroup viewGroup) {
		this.viewGroup = viewGroup;
	}



	/**
	 * 获取本对象的实例
	 * 
	 * @return 本对象的实例
	 */
	public static ViewManager getInstance() {
		if(instance==null){
			instance= new ViewManager();
		}
		return instance;
	}

	/**
	 * 得到Activity的context引用
	 * 
	 * @param activity
	 *            Activity的context
	 */
	public void init(Activity activity) {
		this.activity = activity;
	}
	int n=-1;
	/**
	 * 改变显示布局
	 * 
	 * @param viewGroup
	 *            界面显示的控件容器
	 * @param moduleInfoList
	 *            容器里的控件信息
	 */
	public void changeContentView(ViewGroup viewGroup,
			ArrayList<BaseModuleInfo> moduleInfoList) {
		try{
			if(moduleInfoList==null)return;
			ListIterator<BaseModuleInfo> iterator = moduleInfoList.listIterator();
			while (iterator.hasNext()) {
				BaseModuleInfo baseModuleInfo = iterator.next();
				
				int module_type = baseModuleInfo.getModule_type();
				switch (module_type) { // 根据模块类型，强转。并加入ViewGroup容器
				case ConstantValue.MODULE_TYPE_VIDEO:
					if(ConstantValue.isLocal){
						
						videoInfo = (VideoInfoView) baseModuleInfo;
						BaseCookie.moduleMap.put(videoInfo.getModule_uid(), videoInfo);
						//本地播放器
						lVideo=videoInfo.getView(activity);
						viewGroup.addView(lVideo);
						
						
						//将本地转为直播时候的播放器 ---VLC
						surfaceInfo=changeLocalToLive(videoInfo);
						
						
						//将本地转为直播时候的播放器 ---Vitamio
//						vitamioInfo=changeLocalToVLive(videoInfo);
//						myVideo=vitamioInfo.getView(activity);
//						myVideo.setVisibility(View.GONE);
//						viewGroup.addView(myVideo);
//						surfaceView= surfaceInfo.getView(activity);
//						if(surfaceInfo.isLive()){
//							UserInfoSingleton.putIntAndCommit(CookieConstants.VideoStatus, ConstantValue.VIDEOSTATUS_YES11);
//							//直播
//							lVideo.setVisibility(View.GONE);
//							lVideo.stopPlayback();
//							GlobalParamInfo.getInstance().setLive_url(surfaceInfo.getLive_channel());
//							surfaceInfo.startPlay();
//							
//							UserInfoSingleton.setIslive(ConstantValue.PLAY_LIVE);
//							
//						}else{
//							surfaceView.setVisibility(View.GONE);
//						}
//						
//						viewGroup.addView(surfaceView);
//					
					}
					else{
//						 vvideoInfo = (VVideoInfo) baseModuleInfo;
//						 mVideo=  vvideoInfo.getView(activity);
//						 mVideo.setId(123456);
//						 
//						viewGroup.addView(mVideo);
					}
				
					break;
				case ConstantValue.MODULE_TYPE_SCROLL:
					scrollInfo = (ScrollInfo) baseModuleInfo;
					//将解析的对象存放在Map中
					LogUtil.i(TAG, "scrollInfo.getModule_uid()："+scrollInfo.getModule_uid());
					BaseCookie.moduleMap.put(scrollInfo.getModule_uid(), scrollInfo);
				
					viewGroup.addView(scrollInfo.getView(activity));
					break;
				case ConstantValue.MODULE_TYPE_COUNTDOWN:
					countDownInfo = (CountDownInfo) baseModuleInfo;
					//将解析的对象存放在Map中
					BaseCookie.moduleMap.put(countDownInfo.getModule_uid(), countDownInfo);
					viewGroup.addView(countDownInfo.getView(activity));
					break;
				case ConstantValue.MODULE_TYPE_DATE:
					dateInfo = (DateInfo) baseModuleInfo;
					//将解析的对象存放在Map中
					BaseCookie.moduleMap.put(dateInfo.getModule_uid(), dateInfo);
					viewGroup.addView(dateInfo.getView(activity));
					break;
				case ConstantValue.MODULE_TYPE_TIME:
					timeInfo = (TimeInfo) baseModuleInfo;
					//将解析的对象存放在Map中
					BaseCookie.moduleMap.put(timeInfo.getModule_uid(), timeInfo);
					viewGroup.addView(timeInfo.getView(activity));
					break;
				case ConstantValue.MODULE_TYPE_TEXT:
					textInfo = (TextInfo) baseModuleInfo;
					//将解析的对象存放在Map中
					BaseCookie.moduleMap.put(textInfo.getModule_uid(), textInfo);
					viewGroup.addView(textInfo.getView(activity));
					break;
				case ConstantValue.MODULE_TYPE_PIC:
					picShowInfo = (PicShowInfo) baseModuleInfo;
					//将解析的对象存放在Map中
					BaseCookie.moduleMap.put(picShowInfo.getModule_uid(), picShowInfo);
					viewGroup.addView(picShowInfo.getView(activity));
					break;
				case ConstantValue.MODULE_TYPE_EXIGENT:
					exigentInfo = (ExigentInfo) baseModuleInfo;
					//将解析的对象存放在Map中
					BaseCookie.moduleMap.put(exigentInfo.getModule_uid(), exigentInfo);
					viewGroup.addView(exigentInfo.getView(activity));
					break;
				default:
					break;
				}
			}
			this.viewGroup = viewGroup;
			showContentView();
		}catch(Exception e){
			LogUtil.e(TAG, "changeContentView error ", e);
		} 
		
	}
	/**
	 * 显示紧急消息
	 * 2014-9-24 上午11:06:28
	 * @param viewGroup
	 * @param moduleInfoList
	 *
	 */
	public void changeExigentContentView(ViewGroup viewGroup,
			ArrayList<BaseModuleInfo> moduleInfoList) {
		try{
			if(moduleInfoList==null)return;
			ListIterator<BaseModuleInfo> iterator = moduleInfoList.listIterator();
			while (iterator.hasNext()) {
				BaseModuleInfo baseModuleInfo = iterator.next();
				
				int module_type = baseModuleInfo.getModule_type();
				switch (module_type) { // 根据模块类型，强转。并加入ViewGroup容器
				
				case ConstantValue.MODULE_TYPE_EXIGENT:
					exigentInfo = (ExigentInfo) baseModuleInfo;
					//将解析的对象存放在Map中
					BaseCookie.moduleMap.put(exigentInfo.getModule_uid(), exigentInfo);
					viewGroup.addView(exigentInfo.getView(activity));
					break;
				default:
					break;
				}
			}
			this.viewGroup = viewGroup;
			showContentView();
		}catch(Exception e){
			LogUtil.e(TAG, "changeContentView error ", e);
		} 
		
	}
	/**
	 * 将解析的本地播放的video转为直播的<br>
	 * 2014-4-23 上午11:47:02
	 * @param v
	 * @return
	 *
	 */
	public VideoSurfaceInfo changeLocalToLive(VideoInfoView v){
		VideoSurfaceInfo vv=VideoSurfaceInfo.getInstance();
		if(v==null)return null;
		vv.setModule_type(v.getModule_type());
		vv.setFile_version(v.getFile_version());
		vv.setModule_name(v.getModule_name());
		vv.setModule_uid(v.getModule_uid());
		vv.setModule_gid(v.getModule_gid());
		vv.setModule_Pos(v.getModule_Pos());
		vv.setLive(v.isLive());
		vv.setLive_channel(v.getLive_channel());
		vv.setLive_volume(v.getLive_volume());
		return vv;
	}
	
//	public VideoVitamioInfo changeLocalToVLive(VideoInfoView v){
//		VideoVitamioInfo vv=new VideoVitamioInfo();
//		if(v==null)return null;
//		vv.setModule_type(v.getModule_type());
//		vv.setFile_version(v.getFile_version());
//		vv.setModule_name(v.getModule_name());
//		vv.setModule_uid(v.getModule_uid());
//		vv.setModule_gid(v.getModule_gid());
//		vv.setModule_Pos(v.getModule_Pos());
//		vv.setLive(v.isLive());
//		vv.setLive_channel(v.getLive_channel());
//		vv.setLive_volume(v.getLive_volume());
//		return vv;
//	}
	
	public void removeAllView(){
		if(viewGroup==null)return;
		int count=viewGroup.getChildCount();
		LogUtil.i(TAG, "count="+count);
		for(int i=0;i<count;i++){
			View v=viewGroup.getChildAt(i);
			
			if(v!=null){
				viewGroup.removeViewAt(i);
				LogUtil.i(TAG, "remove MVideo success i="+i);
			}
			
			
		}
	}
	/**
	 * 清除播放控件<br>
	 * 2014-3-24 上午10:59:21
	 *
	 */
	public void removeMVideoView(){
		if(viewGroup==null)return;
		int count=viewGroup.getChildCount();
		LogUtil.i(TAG, "count="+count);
		for(int i=0;i<count;i++){
			View v=viewGroup.getChildAt(i);
			if(v!=null){
				LogUtil.i(TAG, " i="+v.getId());
				if(v.getId()==123456){
					viewGroup.removeViewAt(i);
					LogUtil.i(TAG, "remove MVideo success i="+i);
				}
				
				
			}
			
			
		}
	}
	
	/**
	 * @return the vitamioInfo
	 */
//	public VideoVitamioInfo getVitamioInfo() {
//		return vitamioInfo;
//	}
//	/**
//	 * @param vitamioInfo the vitamioInfo to set
//	 */
//	public void setVitamioInfo(VideoVitamioInfo vitamioInfo) {
//		this.vitamioInfo = vitamioInfo;
//	}
	public void showNullContentView() {
		activity.setContentView(null);
	}
	/**
	 * 显示当前布局
	 */
	private void showContentView() {
		
		if (viewGroup != null) {
			activity.setContentView(viewGroup);
		}
	}
	/**
	 * 默认显示一个背景图<br>
	 * 2014-8-18 下午4:31:42
	 * @param path
	 *
	 */
	@SuppressWarnings("unused")
	private void showContentView(ViewGroup viewGroup) {
		
		if (viewGroup != null) {
			activity.setContentView(viewGroup);
		}
	}

}
